<template>
  <a-spin :spinning="confirmLoading">
    <KFormBuild v-if="formModel" class="k-form-design" ref="kdf" :nodeId="task.taskDefinitionKey" :value="formModel" />
  </a-spin>
</template>

<script>
import { KFormBuild } from '@/components/k-form-design/packages/use.js'
import { getAction } from '@/services/manage'
import { getSelectDictCode, getSelectOptions } from '@/utils/k-form-design'
export default {
  name: 'TaskForm',
  components: {
    KFormBuild
  },
  props: {
    task: {
      type: Object,
      default: () => {}
    },
    formData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      url: {
        taskForm: '/camunda/task/form/model/'
      },
      formModel: {},
      dynamicDataSatrt: {},
      confirmLoading: false
    }
  },
  watch: {
    task: {
      handler() {
        this.getTaskForm()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    async getTaskForm() {
      const res = await getAction(this.url.taskForm + this.task.id)
      if (!res.success) {
        this.confirmLoading = false
        return this.$message.warning('未查询到发起表单')
      }
      const formModel = res.result && res.result.model && JSON.parse(res.result.model)
      if (!formModel) {
        this.$emit('init', false)
        return
      }
      this.$emit('init', true)
      // 在渲染前获取到dynamicData --start
      let dictCodes = getSelectDictCode(formModel.list)
      dictCodes = Array.from(new Set(dictCodes))
      this.dynamicDataSatrt = await getSelectOptions(dictCodes)
      // -- end
      // formModel.list = replaceFormModel(formModel.list, this.task.taskDefinitionKey)

      this.formModel = formModel
      this.$nextTick(() => {
        this.$refs.kdf.setData(this.formData)
      })
    },
    async getData() {
      return this.$refs.kdf.getData()
    }
  }
}
</script>

<style>

</style>
